package nero.yncc.com.createart.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.LinkedHashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityAbout;
import nero.yncc.com.createart.activity.ActivityAddressManager;
import nero.yncc.com.createart.activity.ActivityCollection;
import nero.yncc.com.createart.activity.ActivityMyOrder;
import nero.yncc.com.createart.activity.ActivitySetting;
import nero.yncc.com.createart.activity.ActivitySiteMessage;
import nero.yncc.com.createart.activity.ActivityUserChange;
import nero.yncc.com.createart.utils.ToastUtil;

/**
 * Created by nero on 17-6-23.
 */

public class AdapterUcenter extends RecyclerView.Adapter<AdapterUcenter.Holder> {

    Context context;
    LinkedHashMap<Integer, String> hashMap;

    public AdapterUcenter(Context context, LinkedHashMap<Integer, String> hashMap) {
        this.context = context;
        this.hashMap = hashMap;
        initPopCall();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LinearLayout.inflate(context, R.layout.adapter_ucenter, null);


        Holder holder = new Holder(view);
        return holder;
    }

    PopupWindow popupWindow;

    private void initPopCall() {
        View view_pop = LinearLayout.inflate(context, R.layout.pop_call_us, null);
        popupWindow = new PopupWindow(view_pop, -1, -1);
        view_pop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        TextView tv_call = view_pop.findViewById(R.id.tv_call);
        tv_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "400-400-400"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                popupWindow.dismiss();
            }
        });
        TextView tv_cancle = view_pop.findViewById(R.id.tv_cancle);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        int key = (int) hashMap.keySet().toArray()[position];
        holder.iv_image.setImageResource(key);
        holder.tv_content.setText(hashMap.get(key).toString());
        holder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                switch (position) {
                    case 0:
//                        客户订单
                        context.startActivity(new Intent(context, ActivityMyOrder.class));
                        break;
                    case 1:
//                        关于缕墨
                        context.startActivity(new Intent(context, ActivityAbout.class));
                        break;
                    case 2:
//                        我的收藏

                        context.startActivity(new Intent(context, ActivityCollection.class));
                        break;
                    case 3:
//                        联系我们

                        popupWindow.showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.CENTER, 0, 0);

                        break;
                    case 4:
//                        个人信息
                        Intent intent = new Intent(context, ActivityUserChange.class);
                        context.startActivity(intent);
                        break;
                    case 5:
//                        站内消息
                        Intent intent_site = new Intent(context, ActivitySiteMessage.class);
                        context.startActivity(intent_site);
                        break;
                    case 6:
//                        收货地址
                        context.startActivity(new Intent(context, ActivityAddressManager.class));
                        break;
                    case 7:
//                        设置
                        context.startActivity(new Intent(context, ActivitySetting.class));
                        break;
                    case 8:
//                        退货查询
                        ToastUtil.showToast(context, "暂未开放");
                        break;

                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return hashMap.size();
    }

    class Holder extends RecyclerView.ViewHolder {

        public Holder(View itemView) {
            super(itemView);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_content = itemView.findViewById(R.id.tv_content);
            ll_root = itemView.findViewById(R.id.ll_root);
        }

        final ImageView iv_image;
        final TextView tv_content;
        final LinearLayout ll_root;
    }
}
